<?php

class m130824_122003_create_tables_articles_comment_category extends CDbMigration
{
    public function up()
    {
        $this->createTable('tbl_articles', array(
                                                'id' => 'pk',
                                                'title'=>'string NOT NULL',
                                                'content' => 'text NOT NULL',
                                                'description' => 'text NOT NULL',
                                                'rating'=> 'int(11) DEFAULT NULL',
                                                'image'=>'string NOT NULL',
                                                'create_time' => 'datetime DEFAULT NULL',
                                                'update_time' => 'datetime DEFAULT NULL',
                                                'status'=>'boolean DEFAULT 0',
                                                'category_id'=>'int(11) DEFAULT NULL',
                                                'tags'=>'text NOT NULL',
                                                'create_user_id' => 'int(11) DEFAULT NULL',
                                                'update_user_id' => 'int(11) DEFAULT NULL',
                                           ), 'ENGINE=InnoDB');
        $this->createTable('tbl_comment', array(
                                               'id' => 'pk',
                                               'content' => 'text NOT NULL',
                                               'article_id' => 'int(11) NOT NULL',
                                               'create_time' => 'datetime DEFAULT NULL',
                                               'status'=>'boolean DEFAULT 0',
                                               'create_user_id' => 'int(11) DEFAULT NULL',
                                               'update_user_id' => 'int(11) DEFAULT NULL',
                                               'email'=>'string NOT NULL',
                                          ), 'ENGINE=InnoDB');
        $this->createTable('tbl_category', array(
                                                'id' => 'pk',
                                                'name'=>'string NOT NULL',
                                                'create_time' => 'datetime DEFAULT NULL',
                                                'update_time' => 'datetime DEFAULT NULL',
                                                'tags'=>'text NOT NULL',
                                                'create_user_id' => 'int(11) DEFAULT NULL',
                                                'update_user_id' => 'int(11) DEFAULT NULL',
                                           ), 'ENGINE=InnoDB');

        $this->createTable('tbl_rating', array(
                                              'id'=>'pk',
                                              'article_id'=>'int(11) NOT NULL',
                                              'create_user_id'=>'int(11) NOT NULL',
                                              'value'=>'tinyint(3) NOT NULL',
                                         ),'ENGINE=InnoDB');

        $this->addForeignKey('fk_articles_comment','tbl_comment','article_id','tbl_articles','id','CASCADE','RESTRICT');
        $this->addForeignKey('fk_articles_create_user','tbl_articles','create_user_id','tbl_users','id','RESTRICT','RESTRICT');
        $this->addForeignKey('fk_articles_update_user','tbl_articles','update_user_id','tbl_users','id','RESTRICT','RESTRICT');

        $this->addForeignKey('fk_comment_create_user_id','tbl_comment','create_user_id','tbl_users','id','RESTRICT','RESTRICT');
        $this->addForeignKey('fk_comment_update_user','tbl_comment','update_user_id','tbl_users','id','RESTRICT','RESTRICT');

        $this->addForeignKey('fk_articles_category','tbl_articles','category_id','tbl_category','id','RESTRICT','RESTRICT');
        $this->addForeignKey('fk_category_create_user','tbl_category','create_user_id','tbl_users','id','RESTRICT','RESTRICT');
        $this->addForeignKey('fk_category_update_user','tbl_category','update_user_id','tbl_users','id','RESTRICT','RESTRICT');

        $this->addForeignKey('fk_rating_users','tbl_rating','create_user_id','tbl_users','id','CASCADE','CASCADE');
        $this->addForeignKey('fk_rating_articles','tbl_rating','article_id','tbl_articles','id','CASCADE','CASCADE');
    }

    public function down()
    {
        $this->dropForeignKey('fk_comment_articles', 'tbl_articles');
        $this->dropForeignKey('fk_articles_create_user', 'tbl_articles');
        $this->dropForeignKey('fk_articles_update_user', 'tbl_articles');

        $this->dropForeignKey('fk_comment_create_user_id', 'tbl_comment');
        $this->dropForeignKey('fk_comment_update_user', 'tbl_comment');


        $this->dropForeignKey('fk_category_articles', 'tbl_category');
        $this->dropForeignKey('fk_category_create_user', 'tbl_category');
        $this->dropForeignKey('fk_category_update_user', 'tbl_category');

        $this->dropForeignKey('fk_rating_users', 'tbl_rating');
        $this->dropForeignKey('fk_rating_articles', 'tbl_rating');

        $this->dropTable('tbl_articles');
        $this->dropTable('tbl_comment');
        $this->dropTable('tbl_category');
        $this->dropTable('tbl_rating');
    }

    /*
    // Use safeUp/safeDown to do migration with transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}